const express = require('express'),
    compression = require('compression'),
    cookieParser = require('cookie-parser'),
    morgan = require('morgan'),
    winston = require('winston'),
    session = require('express-session'),
    bodyParser = require('body-parser'),
    // multer = require('multer'),
    nunjucks = require('nunjucks'),

    config = require('./'),
    secret = process.env.secret || 'pipi',
    env = process.env.NODE_ENV || 'development'

module.exports = function(app, passport) {
    app.use(compression({
        threshold: 1
    }))
    app.use(express.static(config.root + '/public'))

    var log = {
        stream: {
            write: message => winston.info(message)
        }
    }

    //log
    // app.use(morgan('combined'))
    // app.use(morgan('combined', log))

    //template
    nunjucks.configure('app/views', {
        express: app
    })

    app.use(cookieParser(secret)) //parse cookie
    app.use(session({
        secret,
        resave: false,
        saveUninitialized: true
    }))

    app.use(bodyParser.json()) //parse application/json
    app.use(bodyParser.urlencoded({ extended: true })) //parse application/x-www-form-urlencoded
        // app.use(multer({ dest: 'uploads/' }).single('pic'))

    //passport
    app.use(passport.initialize())
    app.use(passport.session())
}
